
#Replication code for Sentiment Analysis and Figures (Figure 1 and 2). 'International Organizations in National Parliamentary Debates'. Review of International Organizations. Special io_issue: Domestic Politics and International Organizations.

#First install and load relevant libraries

library(dplyr)
library(quanteda)
library(haven)
library(gridExtra)
library(ggplot2)
library(cowplot)
library(sjPlot)
library(lme4)
library(nlme)
library(ggtext)
library(stargazer)

#Load in the Figure data and the IOParlspeech datasets 

load("Sentiment_Analysis_and_Figures.RData")

ioparlspeech.aus <- readRDS("ioparlspeech.aus.rds")
ioparlspeech.can <- readRDS("ioparlspeech.can.rds")
ioparlspeech.deu <- readRDS("ioparlspeech.deu.rds")
ioparlspeech.gbr <- readRDS("ioparlspeech.gbr.rds")
ioparlspeech.nzl <- readRDS("ioparlspeech.nzl.rds")
ioparlspeech.usa <- readRDS("ioparlspeech.usa.rds")

ioparlspeech <- rbind(ioparlspeech.aus, ioparlspeech.can, ioparlspeech.deu, ioparlspeech.gbr, ioparlspeech.nzl, ioparlspeech.usa)

#Main Regressions

model5 <- lmer(netsent ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)
model6 <- lmer(pos_share ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)
model7 <- lmer(neg_share ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)

model8 <- lmer(pos_share ~ io_authority + in_government + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)
model9 <- lmer(neg_share ~ io_authority + in_government  + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)
model10 <- lmer(netsent ~ io_authority + in_government + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = ioparlspeech)

stargazer(model8, model9, model10, model6, model7, model5)

#Figure 3 (Marginal Effect Plots)

m1 <- plot_model(model8, type = "pred", terms = "io_authority") + labs(title = "Positive Sentiment",  x = "\nio_authority", y = "Pos / Neg Words per IO statement\n") + theme_minimal() + ylim(1, 6.5)
m2 <-  plot_model(model9, type = "pred", terms = "io_authority") + labs(title = "Negative Sentiment",  x = "\nio_authority", y = "\n") + theme_minimal() + ylim(1, 6.5)
m3 <- plot_model(model10, type = "pred", terms = "io_authority") + labs(title = "Net Sentiment",  x = "\nio_authority", y = "\n") + theme_minimal() + ylim(1, 6.5)

grid.arrange(m1, m2, m3, ncol = 3)  

m4 <- plot_model(model8, type = "pred", terms = "in_government", colors = 'black') + labs(title = "Positive Sentiment",  x = "\n", y = "Pos / Neg Words per IO statement\n") + theme_minimal() +  theme(axis.text.x = element_text(size = 12)) + ylim(1, 6.5)
m5 <- plot_model(model9, type = "pred", terms = "in_government", colors = 'black') + labs(title = "Negative Sentiment",  x = "\n", y = "\n") + theme_minimal() +  theme(axis.text.x = element_text(size = 12)) + ylim(1, 6.5)
m6 <- plot_model(model10, type = "pred", terms = "in_government", colors = 'black') + labs(title = "Net Sentiment",  x = "\n", y = "\n") + theme_minimal() +  theme(axis.text.x = element_text(size = 12)) + ylim(1, 6.5)

grid.arrange(m4, m5, m6, ncol = 3)  

#Robustness - Without EU 

no.EU <- subset(ioparlspeech, ioparlspeech$ioname != "EU")

model6.eu <- lmer(netsent ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)
model4.eu <- lmer(pos_share ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)
model5.eu <- lmer(neg_share ~ io_authority + in_government + party_family + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)

model1.eu <- lmer(pos_share ~ io_authority + in_government + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)
model2.eu <- lmer(neg_share ~ io_authority + in_government  + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)
model3.eu <- lmer(netsent ~ io_authority + in_government + year +  iso3country + (1 | speaker) + (1 | party) + member + unemployment + legelec + io_issue, data = no.EU)

stargazer(model1.eu, model2.eu, model3.eu, model4.eu, model5.eu, model6.eu)

#Figure 2

# Plotting Austria 
g1 <- ggplot(summary.aus, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "Austria\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.aus$party_label) + ylim(-0.001, 0.04)

# Plotting Germany
g2 <- ggplot(summary.deu, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "Germany\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.deu$party_label) + ylim(-0.001, 0.04)

# Plotting USA
g3 <- ggplot(summary.usa, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "USA\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.usa$party_label) + ylim(-0.001, 0.04)

# Plotting GBR
g4 <- ggplot(summary.gbr, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "UK\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.gbr$party_label) + ylim(-0.05, 0.04)

# Plotting New Zealand
g5 <- ggplot(summary.nzl, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "New Zealand\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.nzl$party_label) + ylim(-0.001, 0.04)

# Plotting Canada

g6 <- ggplot(summary.can, aes(x = party, y = mean, color = color)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, ymax = ci_upper), width = 0.2) +
  scale_color_identity() + # Use the specified colors
  labs(x = "", y = "Average Net Sentiment\n", title = "Canada\n") +
  theme_minimal() +
  theme(
    axis.text.x = element_markdown(angle = 45, hjust = 1, size = 12)
  ) +
  scale_x_discrete(labels = summary.can$party_label) + ylim(-0.001, 0.04)

#Plot the whole grid

grid.arrange(g6, g5, g3, g1, g2, g4, ncol = 3)

#Figure 1 

#Austria
fg.aus.sent.gov <- ggplot(sent.aus.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "Austria\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.aus.agg.gov.year$year), max(sent.aus.agg.gov.year$year), by = 8), 2018)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))

#Canada
fg.can.sent.gov <- ggplot(sent.can.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "Canada\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.can.agg.gov.year$year), max(sent.can.agg.gov.year$year), by = 10), 2018)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))

#NewZealand

fg.nzl.sent.gov <- ggplot(sent.nzl.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "New Zealand\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.nzl.agg.gov.year$year), max(sent.nzl.agg.gov.year$year), by = 10), 2018)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))

#USA

fg.usa.sent.gov <- ggplot(sent.usa.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "USA\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.usa.agg.gov.year$year), max(sent.usa.agg.gov.year$year), by = 10), 2016)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))

#GBR

fg.gbr.sent.gov <- ggplot(sent.gbr.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "United Kingdom\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.gbr.agg.gov.year$year), max(sent.gbr.agg.gov.year$year), by = 10), 2018)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))


#Germany 

fg.deu.sent.gov <- ggplot(sent.deu.agg.gov.year, aes(x = year, y = net.sentiment, colour = government.status))+
  stat_smooth(method = "loess", formula = y ~ x, span = 0.15, size = 1)+
  ylab("Net Sent (Yearly Avg) \n") + xlab("") +
  theme(plot.title = element_text(size=14, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 12),
        axis.text.x=element_text(angle=90, size=12, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=12, colour = "black"), legend.position = "none") +  
  labs(title = "Germany\n") +
  scale_color_manual(values=c('government'="darkgreen",'opposition'="lightgreen")) +
  scale_x_continuous(breaks = c(seq(min(sent.deu.agg.gov.year$year), max(sent.deu.agg.gov.year$year), by = 10), 2018)) + scale_y_continuous(breaks = c(0, 0.05), limits = c(0, 0.05))

#Plot the whole grid

grid.arrange(fg.can.sent.gov, fg.nzl.sent.gov, fg.usa.sent.gov, fg.aus.sent.gov, fg.deu.sent.gov, fg.gbr.sent.gov, ncol = 3)




